/*
 * IReader.h
 *
 *  Created on: 09.12.2012
 *      Author: caffeine
 */

#ifndef __LAMPE_IREADER_H_
#define __LAMPE_IREADER_H_

#include <lampe/types.h>
#include <lampe/IReferenceCounted.h>


namespace lampe{
namespace stream{


class IReader:public virtual IReferenceCounted{
public:
	virtual bool eof()=0;
	virtual u32 read(void* buffer, u32 size) = 0;
protected:
	template <class T>
	T readVariable(u32*readed){
		T res;
		if (readed)
			*readed=read(&res,sizeof(T));
		else
			read(&res,sizeof(T));
		return res;
	}
public:
	c8 readC8(u32*readed=0){
		return readVariable<c8>(readed);
	}

//unsigned
	u8 readU8(u32*readed=0){
		return readVariable<u8>(readed);
	}

	u16 readU16(u32*readed=0){
		return readVariable<u16>(readed);
	}

	u32 readU32(u32*readed=0){
		return readVariable<u32>(readed);
	}

//signed
	s8 readS8(u32*readed=0){
		return readVariable<s8>(readed);
	}

	s16 readS16(u32*readed=0){
		return readVariable<s16>(readed);
	}

	s32 readS32(u32*readed=0){
		return readVariable<s32>(readed);
	}

//float
	f32 readF64(u32*readed=0){
		return readVariable<f64>(readed);
	}

	f32 readF32(u32*readed=0){
		return readVariable<f32>(readed);
	}
#ifndef NOT_USING_IRRLICHT
//vector float
	Vector3df readVector3df(){
		Vector3df res;
		res.X=readF32();
		res.Y=readF32();
		res.Z=readF32();
		return res;
	}

	Vector2df readVector2df(){
		Vector2df res;
		res.X=readF32();
		res.Y=readF32();
		return res;
	}
//vector int
	Vector2di readVector2di(){
		Vector2di res;
		res.X=readS32();
		res.Y=readS32();
		return res;
	}

	Vector3di readVector3di(){
		Vector3di res;
		res.X=readS32();
		res.Y=readS32();
		res.Z=readS32();
		return res;
	}
#endif
	String readStringc(){
		u32 len=readU32();
		String res;
		char*data=new char[len+1];
		data[len]=char(0);
		read(&data[0],len);
		res.append(data);
		delete []data;
		return res;
	}

String readLine(){
	String res="";
	c8 temp;
	c8 t[2];
	t[1]=char(0);
	do{
		u32 readed=0;
		temp=readC8(&readed);
		if (readed>0){
		if (temp!='\n' && temp!='\r'){
			t[0]=temp;
			res.append(t);
		}
		if (temp==char(10)){
			break;
			return res;
		}
		}else
			break;
	}while(true);
	return res;
}

u32 readLine(c8*data,u32 size){
	c8 temp=char(0);
	u32 p=0;
	do{
		u32 readed=0;
		temp=readC8(&readed);
		if (readed>0){
			//if (temp!='\n' && temp!='\r'){
			data[p]=temp;
			p++;
			size--;
			//}
			if (temp==char(10)){
				//printf("\t\t1P=%u\n",p);
				data[p]=char(0);
				return p;
			}
		}else{
			temp=char(0);
			data[p]=char(0);
			//printf("\t\t2P=%u\n",p);
			return p;
		}
	}while(size>0);
	//printf("\t\t3P=%u\n",p);
	return p;
}

virtual ~IReader(){
}
/*
void readPacket(IPacketWriter*packet){
	packet->grab();
	u32 size=readU32();
	char*buffer=new char[LAMPE_PACKET_BUFFER_SIZE];
	while (size>0){
		u32 readed=size-LAMPE_PACKET_BUFFER_SIZE>0?LAMPE_PACKET_BUFFER_SIZE:LAMPE_PACKET_BUFFER_SIZE-size;
		size-=readed;
		read(buffer,readed);
		packet->write(&buffer[0],readed);
	}
	delete[]buffer;
	packet->drop();
}
*/
};

}//namespace stream
}//namespace lampe

#endif /* __LAMPE_IREADER_H_ */
